 <?php


class Pagination 
{
	var $page = 1; // Current Page
    var $perPage = 10; // Items on each page, defaulted to 10
    var $showFirstAndLast = false; // if you would like the first and last page options.
	function getStart( $page = 1, $perPage = 10, $total=0, $url='#')
    {
      // Assign the items per page variable
      if (!empty($perPage))
        $this->perPage = $perPage;
      
      // Assign the page variable
      if ( !empty($this->page)) {
      	$this->page = $page; // if we don't have a page number then assume we are on the first page
      }
       
      // Take the length of the array
      $this->length = $total;
      
      // Get the number of pages
      $this->pages = ceil($this->length / $this->perPage);
      
      // Calculate the starting point 
      $this->start  = ceil(($this->page - 1) * $this->perPage);
	  $this->url = $url;
      
      // Return the part of the array we have requested
      return   $this->start;
    }
	function links($offset)
	{
		$page = $this->page;
	 	$noofpages = $this->pages;
		$actpagename = $this->url;
		$retstr='';
		
		if( $this->length == 0) return '';
		
		$queryURL = '';
		if (count($_GET)) {
			foreach ($_GET as $key => $value) {
			  if ($key != 'page') {
				$queryURL .= '&'.$key.'='.$value;
			  }
			}
		} 
	  
		$noofpages = round($noofpages);
		$prev=$page-1;
		$next=$page+1;
			
		$pagesstart = $page - floor($offset/2);
		if ($pagesstart < 1) $pagesstart = 1;
		$pagesend = $pagesstart+$offset-1;	
		if ($pagesend > $noofpages) {  $pagesend = $noofpages; $pagesstart = $pagesend - $offset+1; 
		if ($pagesstart < 1) $pagesstart = 1;}	
			
			if ($page != "1" && round($noofpages) != "0") 
			{
				$retstr .=	"<li><a href='$actpagename&page=$prev&$queryURL'  >Prev</a>&nbsp;</li>";
			}
			for($i = 1; $i <= $noofpages; $i++)
			{ 
				if ($pagesstart <= $i && $pagesend >= $i) 
				{
					if ($i == $page) 
					{
						$retstr .="<li><a class='active'>$i</a>&nbsp;</li>";
					}
					else 
					{
					
						$retstr .="<li><a href='$actpagename&page=$i&$queryURL'>$i</a>&nbsp;</li>";
					
					}
				}
			}
			if (round($noofpages) == "0") 
			{
				$retstr .= $i;
			}
	
			if ($page != round($noofpages) && round($noofpages) != "0") 
			{
			
				$retstr .="<li><a href='$actpagename&page=$next&$queryURL' class='pagingdarkbluelink'>Next</a></li>";
			
			}
			return '<ul>'.$retstr.'</ul>';
		
	} // if($ctype
}
?>